package com.pig.book;

import com.pig.util.kuaipan.oauth.KuaiPanAccessToken;
import com.pig.util.kuaipan.oauth.KuaiPanRequestToken;
import com.pig.util.kuaipan.oauth.KuaiPanAccessToken.KuaiPanAccessTokenInterface;
import com.pig.util.kuaipan.oauth.KuaiPanRequestToken.KuaiPanRequestTokenInterface;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Login extends Activity {

	private WebView webView;
	
	private KuaiPanAccessToken kuaiPanAccessToken;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		webView = (WebView) findViewById(R.id.wvLogin);
		webView.clearCache(true);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setBuiltInZoomControls(true);
		webView.addJavascriptInterface(new JavaScriptInterface(), "KuaiPanAuthorize");
		webView.setWebViewClient(webViewClient);
		
		kuaiPanAccessToken = new KuaiPanAccessToken(kuaiPanAccessTokenInterface);
		
		KuaiPanRequestToken kuaiPanRequestToken = new KuaiPanRequestToken(kuaiPanRequestTokenInterface);
		kuaiPanRequestToken.requestToken();
		
		
	}
	
	private WebViewClient webViewClient = new WebViewClient(){

		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			
			//view.loadUrl("javascript:window.KuaiPanAuthorize.getHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
			
			super.onPageFinished(view, url);
			System.out.println("onPageFinished...");
			
			if(hasAuthorize){
				view.loadUrl("javascript:window.KuaiPanAuthorize.getHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
				
			}
			hasAuthorize = true;
			
		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			// TODO Auto-generated method stub
			super.onPageStarted(view, url, favicon);
			System.out.println("onPageStarted...");
			
		}

		@Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
			// TODO Auto-generated method stub
			//super.onReceivedSslError(view, handler, error);
			
			handler.proceed();
			System.out.println("onReceivedSslError...");
			
		}

		@Override
		public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
			// TODO Auto-generated method stub
			return super.shouldOverrideKeyEvent(view, event);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			//return super.shouldOverrideUrlLoading(view, url);
			
			view.loadUrl(url);
			return true;
		}
		
	};
	
	class JavaScriptInterface{
		
		public void getHTML(String html){
			//System.out.println("html: " + html);
			
			kuaiPanAccessToken.accessToken(requestToken_OAuthToken, requestToken_OAuthToken_Secret);
			
			
		}
	}
	
	//
	private String requestToken_OAuthToken;
	private String requestToken_OAuthToken_Secret;
	private boolean hasAuthorize = false;
	
	private KuaiPanRequestTokenInterface kuaiPanRequestTokenInterface = new KuaiPanRequestTokenInterface(){

		public void requestTokenFailWithError(KuaiPanRequestToken kuaiPanRequestToken, String errorMsg) {
			// TODO Auto-generated method stub
			System.out.println("errorMsg: " + errorMsg);
		}

		public void requestTokenFinishWithResult(String oauthToken, String oauthTokenSecret) {
			// TODO Auto-generated method stub
			requestToken_OAuthToken = oauthToken;
			requestToken_OAuthToken_Secret = oauthTokenSecret;
			
			webView.loadUrl("https://www.kuaipan.cn/api.php?ac=open&op=authorise&oauth_token=" + oauthToken);
			
			
		}
		
	};
	
	//
	
	private KuaiPanAccessTokenInterface kuaiPanAccessTokenInterface = new KuaiPanAccessTokenInterface(){

		public void accessTokenFailWithError(KuaiPanAccessToken kuaiPanAccessToken, String errorMsg) {
			// TODO Auto-generated method stub
			
			finish();
			
		}

		public void accessTokenFinishWithResult(String oauthToken, String oauthTokenSecret, String userID, String chargedDir) {
			// TODO Auto-generated method stub
			System.out.println("login success...");
			
			SharedPreferences sharedPrefs = getSharedPreferences("kuaipan_oauth_preferences", Context.MODE_PRIVATE);
	        sharedPrefs.edit().putString("oauth_token", oauthToken);
	        sharedPrefs.edit().putString("oauth_token_secret", oauthTokenSecret);
	        sharedPrefs.edit().putString("user_id", userID);
	        sharedPrefs.edit().putString("charged_dir", chargedDir);
	        
	        //
			Intent intent = new Intent(Login.this, RootPageByActivityGroupAndButtonBar.class);
			startActivity(intent);
			overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			finish();
			
		}
		
	};
	
}
